//
//  YTChangePhoneController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/7.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTChangePhoneController.h"

@interface YTChangePhoneController ()
@property (weak, nonatomic) IBOutlet UITextField *moblieTxtField;
@property (weak, nonatomic) IBOutlet UIButton *verityMobileButton;
@property (weak, nonatomic) IBOutlet UITextField *verityTxtField;
@property (weak, nonatomic) IBOutlet UIButton *getVerityCodeButton;

@end

@implementation YTChangePhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"倛袨1"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self action:@selector(backButtonClick)];
    
    [self.moblieTxtField addTarget:self action:@selector(verfyPhoneTxt:) forControlEvents:UIControlEventEditingChanged];
}
#pragma mark: - 手机号txtfield监听
- (void)verfyPhoneTxt:(UITextField *)textField
{
    self.verityMobileButton.selected = [textField.text phoneNumberIsTrue];//[self verifyMobile:textField.text];
}

#pragma mark: - 获取验证码 按钮监听
- (IBAction)getCodeButtonClick:(JKCountDownButton *)sender
{
    if (!self.verityMobileButton.selected) {
        [self.view showTipWithTipStr:@"请输入正确的手机号"];
        return;
    }
    
    sender.enabled = YES;
    //button type要 设置成custom 否则会闪动
    [sender startCountDownWithSecond:60];
    // 获取当前时间错
    NSString *timestamp = [NSString getNowTime];
    
    // 生成签名
    NSString *sign = [[NSString stringWithFormat:@"%@%@%@%@",kGetVerifiCodeKey,timestamp,self.moblieTxtField.text,@"2"] md5];
    
    // 发送验证码
    [YTNetworkManager.shareManager requestVerifyCodeWithMobile:self.moblieTxtField.text
                                                          type:@"2"
                                                    timestamp:timestamp
                                                          sign:sign
                                                    completion:^(BOOL isSucc, NSString *statusInfo)
     {
         if (!isSucc) {
             [self.view showTipWithTipStr:statusInfo];
             sender.enabled = NO;
             return ;
         }
         
         [self.view showTipWithTipStr:statusInfo];
         sender.enabled = NO;
         
     }];
    
    [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
        NSString *title = [NSString stringWithFormat:@"剩余%zd秒",second];
        return title;
    }];
    
    [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        return @"点击重新获取";
    }];
}

#pragma mark: - 更改手机号 监听
- (IBAction)changeMoblieButtonClick:(UIButton *)sender
{
    if ((!self.verityTxtField.hasText) || (!self.moblieTxtField.hasText)) {
        [self.view showTipWithTipStr:@"请输入验证码或手机号"];
        return;
    }
    
    [YTNetworkManager.shareManager requestChangeMoblie:self.moblieTxtField.text
                                                  code:self.verityTxtField.text
                                            completion:^(BOOL isSucc, NSString *statusInfo)
    {
        if (!isSucc) {
            [self.view showTipWithTipStr:statusInfo];
            return ;
        }
        
        [self.view showTipWithTipStr:statusInfo];
        [self.navigationController popViewControllerAnimated:true];
        
    }];
}

#pragma mark: - 返回按钮 监听
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

@end
